/* eslint-disable no-useless-escape */
import type { languages } from "monaco-editor";

export const sqlConf: languages.LanguageConfiguration = {
  comments: {
    lineComment: "--",
    blockComment: ["/*", "*/"],
  },
  brackets: [
    ["{", "}"],
    ["[", "]"],
    ["(", ")"],
  ],
  autoClosingPairs: [
    { open: "{", close: "}" },
    { open: "[", close: "]" },
    { open: "(", close: ")" },
    { open: '"', close: '"' },
    { open: "'", close: "'" },
  ],
  surroundingPairs: [
    { open: "{", close: "}" },
    { open: "[", close: "]" },
    { open: "(", close: ")" },
    { open: '"', close: '"' },
    { open: "'", close: "'" },
  ],
};

// WIP
// DuckDB keywords: select list(upper(keyword_name)) from duckdb_keywords();
// DuckDB function: select list(distinct upper(function_name)) from duckdb_functions();
export const sqlDef = <languages.IMonarchLanguage>{
  defaultToken: "",
  tokenPostfix: ".sql",
  ignoreCase: true,

  brackets: [
    { open: "[", close: "]", token: "delimiter.square" },
    { open: "(", close: ")", token: "delimiter.parenthesis" },
  ],

  keywords: [
    "ABORT",
    "ABSOLUTE",
    "ACCESS",
    "ACTION",
    "ADD",
    "ADMIN",
    "AFTER",
    "AGGREGATE",
    "ALL",
    "ALSO",
    "ALTER",
    "ALWAYS",
    "ANALYSE",
    "ANALYZE",
    "AND",
    "ANTI",
    "ANY",
    "ARRAY",
    "AS",
    "ASC",
    "ASOF",
    "ASSERTION",
    "ASSIGNMENT",
    "ASYMMETRIC",
    "AT",
    "ATTACH",
    "ATTRIBUTE",
    "AUTHORIZATION",
    "BACKWARD",
    "BEFORE",
    "BEGIN",
    "BETWEEN",
    "BIGINT",
    "BINARY",
    "BIT",
    "BOOLEAN",
    "BOTH",
    "BY",
    "CACHE",
    "CALL",
    "CALLED",
    "CASCADE",
    "CASCADED",
    "CASE",
    "CAST",
    "CATALOG",
    "CENTURIES",
    "CENTURY",
    "CHAIN",
    "CHAR",
    "CHARACTER",
    "CHARACTERISTICS",
    "CHECK",
    "CHECKPOINT",
    "CLASS",
    "CLOSE",
    "CLUSTER",
    "COALESCE",
    "COLLATE",
    "COLLATION",
    "COLUMN",
    "COLUMNS",
    "COMMENT",
    "COMMENTS",
    "COMMIT",
    "COMMITTED",
    "COMPRESSION",
    "CONCURRENTLY",
    "CONFIGURATION",
    "CONFLICT",
    "CONNECTION",
    "CONSTRAINT",
    "CONSTRAINTS",
    "CONTENT",
    "CONTINUE",
    "CONVERSION",
    "COPY",
    "COST",
    "CREATE",
    "CROSS",
    "CSV",
    "CUBE",
    "CURRENT",
    "CURSOR",
    "CYCLE",
    "DATA",
    "DATABASE",
    "DAY",
    "DAYS",
    "DEALLOCATE",
    "DEC",
    "DECADE",
    "DECADES",
    "DECIMAL",
    "DECLARE",
    "DEFAULT",
    "DEFAULTS",
    "DEFERRABLE",
    "DEFERRED",
    "DEFINER",
    "DELETE",
    "DELIMITER",
    "DELIMITERS",
    "DEPENDS",
    "DESC",
    "DESCRIBE",
    "DETACH",
    "DICTIONARY",
    "DISABLE",
    "DISCARD",
    "DISTINCT",
    "DO",
    "DOCUMENT",
    "DOMAIN",
    "DOUBLE",
    "DROP",
    "EACH",
    "ELSE",
    "ENABLE",
    "ENCODING",
    "ENCRYPTED",
    "END",
    "ENUM",
    "ESCAPE",
    "EVENT",
    "EXCEPT",
    "EXCLUDE",
    "EXCLUDING",
    "EXCLUSIVE",
    "EXECUTE",
    "EXISTS",
    "EXPLAIN",
    "EXPORT",
    "EXPORT_STATE",
    "EXTENSION",
    "EXTERNAL",
    "EXTRACT",
    "FALSE",
    "FAMILY",
    "FETCH",
    "FILTER",
    "FIRST",
    "FLOAT",
    "FOLLOWING",
    "FOR",
    "FORCE",
    "FOREIGN",
    "FORWARD",
    "FREEZE",
    "FROM",
    "FULL",
    "FUNCTION",
    "FUNCTIONS",
    "GENERATED",
    "GLOB",
    "GLOBAL",
    "GRANT",
    "GRANTED",
    "GROUP",
    "GROUPING",
    "GROUPING_ID",
    "GROUPS",
    "HANDLER",
    "HAVING",
    "HEADER",
    "HOLD",
    "HOUR",
    "HOURS",
    "IDENTITY",
    "IF",
    "IGNORE",
    "ILIKE",
    "IMMEDIATE",
    "IMMUTABLE",
    "IMPLICIT",
    "IMPORT",
    "IN",
    "INCLUDE",
    "INCLUDING",
    "INCREMENT",
    "INDEX",
    "INDEXES",
    "INHERIT",
    "INHERITS",
    "INITIALLY",
    "INLINE",
    "INNER",
    "INOUT",
    "INPUT",
    "INSENSITIVE",
    "INSERT",
    "INSTALL",
    "INSTEAD",
    "INT",
    "INTEGER",
    "INTERSECT",
    "INTERVAL",
    "INTO",
    "INVOKER",
    "IS",
    "ISNULL",
    "ISOLATION",
    "JOIN",
    "JSON",
    "KEY",
    "LABEL",
    "LANGUAGE",
    "LARGE",
    "LAST",
    "LATERAL",
    "LEADING",
    "LEAKPROOF",
    "LEFT",
    "LEVEL",
    "LIKE",
    "LIMIT",
    "LISTEN",
    "LOAD",
    "LOCAL",
    "LOCATION",
    "LOCK",
    "LOCKED",
    "LOGGED",
    "MACRO",
    "MAP",
    "MAPPING",
    "MATCH",
    "MATERIALIZED",
    "MAXVALUE",
    "METHOD",
    "MICROSECOND",
    "MICROSECONDS",
    "MILLENNIA",
    "MILLENNIUM",
    "MILLISECOND",
    "MILLISECONDS",
    "MINUTE",
    "MINUTES",
    "MINVALUE",
    "MODE",
    "MONTH",
    "MONTHS",
    "MOVE",
    "NAME",
    "NAMES",
    "NATIONAL",
    "NATURAL",
    "NCHAR",
    "NEW",
    "NEXT",
    "NO",
    "NONE",
    "NOT",
    "NOTHING",
    "NOTIFY",
    "NOTNULL",
    "NOWAIT",
    "NULL",
    "NULLIF",
    "NULLS",
    "NUMERIC",
    "OBJECT",
    "OF",
    "OFF",
    "OFFSET",
    "OIDS",
    "OLD",
    "ON",
    "ONLY",
    "OPERATOR",
    "OPTION",
    "OPTIONS",
    "OR",
    "ORDER",
    "ORDINALITY",
    "OTHERS",
    "OUT",
    "OUTER",
    "OVER",
    "OVERLAPS",
    "OVERLAY",
    "OVERRIDING",
    "OWNED",
    "OWNER",
    "PARALLEL",
    "PARSER",
    "PARTIAL",
    "PARTITION",
    "PASSING",
    "PASSWORD",
    "PERCENT",
    "PERSISTENT",
    "PIVOT",
    "PIVOT_LONGER",
    "PIVOT_WIDER",
    "PLACING",
    "PLANS",
    "POLICY",
    "POSITION",
    "POSITIONAL",
    "PRAGMA",
    "PRECEDING",
    "PRECISION",
    "PREPARE",
    "PREPARED",
    "PRESERVE",
    "PRIMARY",
    "PRIOR",
    "PRIVILEGES",
    "PROCEDURAL",
    "PROCEDURE",
    "PROGRAM",
    "PUBLICATION",
    "QUALIFY",
    "QUOTE",
    "RANGE",
    "READ",
    "REAL",
    "REASSIGN",
    "RECHECK",
    "RECURSIVE",
    "REF",
    "REFERENCES",
    "REFERENCING",
    "REFRESH",
    "REINDEX",
    "RELATIVE",
    "RELEASE",
    "RENAME",
    "REPEATABLE",
    "REPLACE",
    "REPLICA",
    "RESET",
    "RESPECT",
    "RESTART",
    "RESTRICT",
    "RETURNING",
    "RETURNS",
    "REVOKE",
    "RIGHT",
    "ROLE",
    "ROLLBACK",
    "ROLLUP",
    "ROW",
    "ROWS",
    "RULE",
    "SAMPLE",
    "SAVEPOINT",
    "SCHEMA",
    "SCHEMAS",
    "SCOPE",
    "SCROLL",
    "SEARCH",
    "SECOND",
    "SECONDS",
    "SECRET",
    "SECURITY",
    "SELECT",
    "SEMI",
    "SEQUENCE",
    "SEQUENCES",
    "SERIALIZABLE",
    "SERVER",
    "SESSION",
    "SET",
    "SETOF",
    "SETS",
    "SHARE",
    "SHOW",
    "SIMILAR",
    "SIMPLE",
    "SKIP",
    "SMALLINT",
    "SNAPSHOT",
    "SOME",
    "SQL",
    "STABLE",
    "STANDALONE",
    "START",
    "STATEMENT",
    "STATISTICS",
    "STDIN",
    "STDOUT",
    "STORAGE",
    "STORED",
    "STRICT",
    "STRIP",
    "STRUCT",
    "SUBSCRIPTION",
    "SUBSTRING",
    "SUMMARIZE",
    "SYMMETRIC",
    "SYSID",
    "SYSTEM",
    "TABLE",
    "TABLES",
    "TABLESAMPLE",
    "TABLESPACE",
    "TEMP",
    "TEMPLATE",
    "TEMPORARY",
    "TEXT",
    "THEN",
    "TIES",
    "TIME",
    "TIMESTAMP",
    "TO",
    "TRAILING",
    "TRANSACTION",
    "TRANSFORM",
    "TREAT",
    "TRIGGER",
    "TRIM",
    "TRUE",
    "TRUNCATE",
    "TRUSTED",
    "TRY_CAST",
    "TYPE",
    "TYPES",
    "UNBOUNDED",
    "UNCOMMITTED",
    "UNENCRYPTED",
    "UNION",
    "UNIQUE",
    "UNKNOWN",
    "UNLISTEN",
    "UNLOGGED",
    "UNPIVOT",
    "UNTIL",
    "UPDATE",
    "USE",
    "USER",
    "USING",
    "VACUUM",
    "VALID",
    "VALIDATE",
    "VALIDATOR",
    "VALUE",
    "VALUES",
    "VARCHAR",
    "VARIADIC",
    "VARYING",
    "VERBOSE",
    "VERSION",
    "VIEW",
    "VIEWS",
    "VIRTUAL",
    "VOLATILE",
    "WEEK",
    "WEEKS",
    "WHEN",
    "WHERE",
    "WHITESPACE",
    "WINDOW",
    "WITH",
    "WITHIN",
    "WITHOUT",
    "WORK",
    "WRAPPER",
    "WRITE",
    "XML",
    "XMLATTRIBUTES",
    "XMLCONCAT",
    "XMLELEMENT",
    "XMLEXISTS",
    "XMLFOREST",
    "XMLNAMESPACES",
    "XMLPARSE",
    "XMLPI",
    "XMLROOT",
    "XMLSERIALIZE",
    "XMLTABLE",
    "YEAR",
    "YEARS",
    "YES",
    "ZONE",
  ],
  operators: [
    // Logical
    "ALL",
    "AND",
    "ANY",
    "BETWEEN",
    "EXISTS",
    "IN",
    "LIKE",
    "NOT",
    "OR",
    "SOME",
    // Set
    "EXCEPT",
    "INTERSECT",
    "UNION",
    // Join
    "APPLY",
    "CROSS",
    "FULL",
    "INNER",
    "JOIN",
    "LEFT",
    "OUTER",
    "RIGHT",
    // Predicates
    "CONTAINS",
    "FREETEXT",
    "IS",
    "NULL",
    // Pivoting
    "PIVOT",
    "UNPIVOT",
    // Merging
    "MATCHED",
  ],
  builtinFunctions: [
    "SNIFF_CSV",
    "ARROW_SCAN",
    "ARROW_SCAN_DUMB",
    "CHECKPOINT",
    "DUCKDB_COLUMNS",
    "DUCKDB_CONSTRAINTS",
    "DUCKDB_DATABASES",
    "DUCKDB_DEPENDENCIES",
    "DUCKDB_EXTENSIONS",
    "DUCKDB_FUNCTIONS",
    "DUCKDB_INDEXES",
    "DUCKDB_KEYWORDS",
    "DUCKDB_MEMORY",
    "DUCKDB_OPTIMIZERS",
    "DUCKDB_SCHEMAS",
    "DUCKDB_SECRETS",
    "DUCKDB_SEQUENCES",
    "DUCKDB_SETTINGS",
    "DUCKDB_TABLES",
    "DUCKDB_TEMPORARY_FILES",
    "DUCKDB_TYPES",
    "DUCKDB_VIEWS",
    "FORCE_CHECKPOINT",
    "GENERATE_SERIES",
    "GLOB",
    "ICU_CALENDAR_NAMES",
    "INDEX_SCAN",
    "PG_TIMEZONE_NAMES",
    "PRAGMA_COLLATIONS",
    "PRAGMA_DATABASE_SIZE",
    "PRAGMA_METADATA_INFO",
    "PRAGMA_PLATFORM",
    "PRAGMA_SHOW",
    "PRAGMA_STORAGE_INFO",
    "PRAGMA_TABLE_INFO",
    "PRAGMA_USER_AGENT",
    "PRAGMA_VERSION",
    "RANGE",
    "READ_BLOB",
    "READ_CSV",
    "READ_CSV_AUTO",
    "READ_TEXT",
    "REPEAT",
    "REPEAT_ROW",
    "SEQ_SCAN",
    "UNNEST",
    "TEST_VECTOR_TYPES",
    "TEST_ALL_TYPES",
    "SUMMARY",
    "ENUM_RANGE",
    "MAP_ENTRIES",
    "FROM_BINARY",
    "STRUCT_EXTRACT",
    "STRPTIME",
    "STRPOS",
    "STRLEN",
    "STRIP_ACCENTS",
    "STRING_TO_ARRAY",
    "STRING_SPLIT_REGEX",
    "STRING_SPLIT",
    "STRFTIME",
    "STATS",
    "STARTS_WITH",
    "SQRT",
    "STR_SPLIT_REGEX",
    "SPLIT",
    "SIN",
    "SIGNBIT",
    "SIGN",
    "SHA256",
    "SET_BIT",
    "SETSEED",
    "SUBSTR",
    "SECOND",
    "RTRIM",
    "RPAD",
    "ROW",
    "SUBSTRING",
    "SUBSTRING_GRAPHEME",
    "ROUND",
    "RIGHT_GRAPHEME",
    "RIGHT",
    "REVERSE",
    "REPLACE",
    "SUBTRACT",
    "REGEXP_SPLIT_TO_ARRAY",
    "REGEXP_REPLACE",
    "REGEXP_MATCHES",
    "REGEXP_FULL_MATCH",
    "REGEXP_EXTRACT_ALL",
    "REGEXP_EXTRACT",
    "MICROSECOND",
    "ENUM_LAST",
    "~~~",
    "~~*",
    "~~",
    "~",
    "||",
    "|",
    "__INTERNAL_DECOMPRESS_STRING",
    "__INTERNAL_DECOMPRESS_INTEGRAL_USMALLINT",
    "__INTERNAL_DECOMPRESS_INTEGRAL_UINTEGER",
    "__INTERNAL_DECOMPRESS_INTEGRAL_UHUGEINT",
    "__INTERNAL_DECOMPRESS_INTEGRAL_UBIGINT",
    "__INTERNAL_DECOMPRESS_INTEGRAL_SMALLINT",
    "__INTERNAL_DECOMPRESS_INTEGRAL_INTEGER",
    "__INTERNAL_DECOMPRESS_INTEGRAL_HUGEINT",
    "__INTERNAL_DECOMPRESS_INTEGRAL_BIGINT",
    "__INTERNAL_COMPRESS_STRING_UTINYINT",
    "__INTERNAL_COMPRESS_STRING_USMALLINT",
    "__INTERNAL_COMPRESS_STRING_UINTEGER",
    "__INTERNAL_COMPRESS_STRING_UBIGINT",
    "!__POSTFIX",
    "!~~",
    "!~~*",
    "%",
    "&",
    "__INTERNAL_COMPRESS_STRING_HUGEINT",
    "*",
    "**",
    "+",
    "-",
    "/",
    "//",
    "<#>",
    "<->",
    "<<",
    "<=>",
    ">>",
    "@",
    "ABS",
    "ACOS",
    "ADD",
    "AGE",
    "AGGREGATE",
    "ALIAS",
    "REGEXP_ESCAPE",
    "APPLY",
    "REDUCE",
    "RANDOM",
    "RADIANS",
    "QUARTER",
    "PRINTF",
    "PREFIX",
    "POWER",
    "POW",
    "POSITION",
    "ARRAY_AGGR",
    "ARRAY_AGGREGATE",
    "ARRAY_APPLY",
    "ARRAY_CAT",
    "ARRAY_CONCAT",
    "ARRAY_CONTAINS",
    "ARRAY_COSINE_SIMILARITY",
    "ARRAY_CROSS_PRODUCT",
    "ARRAY_DISTANCE",
    "ARRAY_DISTINCT",
    "ARRAY_DOT_PRODUCT",
    "ARRAY_EXTRACT",
    "ARRAY_FILTER",
    "ARRAY_GRADE_UP",
    "ARRAY_HAS",
    "^@",
    "ARRAY_INDEXOF",
    "ARRAY_INNER_PRODUCT",
    "^",
    "ARRAY_LENGTH",
    "YEARWEEK",
    "YEAR",
    "ARRAY_POSITION",
    "XOR",
    "WHICH_SECRET",
    "WEEKOFYEAR",
    "ARRAY_REDUCE",
    "ARRAY_RESIZE",
    "WEEKDAY",
    "ARRAY_REVERSE_SORT",
    "ARRAY_SELECT",
    "ARRAY_SLICE",
    "ARRAY_SORT",
    "WEEK",
    "ARRAY_TRANSFORM",
    "ARRAY_UNIQUE",
    "ARRAY_VALUE",
    "ARRAY_WHERE",
    "ARRAY_ZIP",
    "ASCII",
    "ASIN",
    "ATAN",
    "ATAN2",
    "PI",
    "BAR",
    "BASE64",
    "BIN",
    "BITSTRING",
    "PARSE_PATH",
    "PARSE_FILENAME",
    "BIT_COUNT",
    "BIT_LENGTH",
    "PARSE_DIRPATH",
    "BIT_POSITION",
    "PARSE_DIRNAME",
    "ORD",
    "OCTET_LENGTH",
    "CARDINALITY",
    "CBRT",
    "CEIL",
    "CEILING",
    "CENTURY",
    "CHR",
    "COMBINE",
    "CONCAT",
    "CONCAT_WS",
    "CONSTANT_OR_NULL",
    "CONTAINS",
    "SUFFIX",
    "COS",
    "COT",
    "NOW",
    "VERSION",
    "NOT_LIKE_ESCAPE",
    "NOT_ILIKE_ESCAPE",
    "NFC_NORMALIZE",
    "CREATE_SORT_KEY",
    "VECTOR_TYPE",
    "CURRENT_DATABASE",
    "CURRENT_DATE",
    "CURRENT_LOCALTIME",
    "CURRENT_LOCALTIMESTAMP",
    "CURRENT_QUERY",
    "NEXTVAL",
    "CURRENT_SCHEMA",
    "CURRENT_SCHEMAS",
    "MAP_EXTRACT",
    "NEXTAFTER",
    "CURRVAL",
    "DAMERAU_LEVENSHTEIN",
    "DATEDIFF",
    "DATEPART",
    "DATESUB",
    "DATETRUNC",
    "MULTIPLY",
    "DATE_DIFF",
    "DATE_PART",
    "DATE_SUB",
    "DATE_TRUNC",
    "DAY",
    "DAYNAME",
    "DAYOFMONTH",
    "DAYOFWEEK",
    "DAYOFYEAR",
    "DECADE",
    "DECODE",
    "DEGREES",
    "DIVIDE",
    "EDITDIST3",
    "ELEMENT_AT",
    "ENCODE",
    "ENDS_WITH",
    "MONTHNAME",
    "ENUM_CODE",
    "ENUM_FIRST",
    "STRUCT_INSERT",
    "STR_SPLIT",
    "ENUM_RANGE_BOUNDARY",
    "EPOCH",
    "EPOCH_MS",
    "EPOCH_NS",
    "EPOCH_US",
    "ERA",
    "ERROR",
    "EVEN",
    "EXP",
    "FACTORIAL",
    "MONTH",
    "UUID",
    "FILTER",
    "FINALIZE",
    "MOD",
    "FLATTEN",
    "FLOOR",
    "UPPER",
    "FORMAT",
    "FORMATREADABLEDECIMALSIZE",
    "FORMATREADABLESIZE",
    "FORMAT_BYTES",
    "FROM_BASE64",
    "STRUCT_PACK",
    "FROM_HEX",
    "MISMATCHES",
    "GAMMA",
    "GCD",
    "UNPIVOT_LIST",
    "GEN_RANDOM_UUID",
    "UNION_VALUE",
    "UNION_TAG",
    "GET_BIT",
    "UNION_EXTRACT",
    "GET_CURRENT_TIME",
    "GET_CURRENT_TIMESTAMP",
    "GRADE_UP",
    "GREATEST",
    "GREATEST_COMMON_DIVISOR",
    "MINUTE",
    "HAMMING",
    "HASH",
    "HEX",
    "MILLISECOND",
    "HOUR",
    "ICU_SORT_KEY",
    "ILIKE_ESCAPE",
    "INSTR",
    "IN_SEARCH_PATH",
    "ISFINITE",
    "ISINF",
    "ISNAN",
    "ISODOW",
    "ISOYEAR",
    "JACCARD",
    "JARO_SIMILARITY",
    "JARO_WINKLER_SIMILARITY",
    "JULIAN",
    "MILLENNIUM",
    "MD5_NUMBER_UPPER",
    "MD5_NUMBER_LOWER",
    "MD5_NUMBER",
    "LAST_DAY",
    "LCASE",
    "LCM",
    "LEAST",
    "LEAST_COMMON_MULTIPLE",
    "LEFT",
    "LEFT_GRAPHEME",
    "LEN",
    "LENGTH",
    "LENGTH_GRAPHEME",
    "LEVENSHTEIN",
    "LGAMMA",
    "LIKE_ESCAPE",
    "MD5",
    "MAP_VALUES",
    "LIST_AGGR",
    "LIST_AGGREGATE",
    "UNICODE",
    "UNHEX",
    "LIST_APPLY",
    "UNBIN",
    "UCASE",
    "TYPEOF",
    "TXID_CURRENT",
    "TRY_STRPTIME",
    "TRUNC",
    "TRIM",
    "LIST_CAT",
    "LIST_CONCAT",
    "LIST_CONTAINS",
    "LIST_COSINE_SIMILARITY",
    "TRANSLATE",
    "LIST_DISTANCE",
    "LIST_DISTINCT",
    "LIST_DOT_PRODUCT",
    "LIST_ELEMENT",
    "TRANSACTION_TIMESTAMP",
    "LIST_EXTRACT",
    "LIST_FILTER",
    "TO_YEARS",
    "LIST_GRADE_UP",
    "LIST_HAS",
    "TO_WEEKS",
    "TO_TIMESTAMP",
    "TO_SECONDS",
    "LIST_INDEXOF",
    "LIST_INNER_PRODUCT",
    "TO_MONTHS",
    "TO_MINUTES",
    "TO_MILLISECONDS",
    "TO_MILLENNIA",
    "TO_MICROSECONDS",
    "TO_HOURS",
    "TO_HEX",
    "TO_DECADES",
    "TO_DAYS",
    "LIST_PACK",
    "LIST_POSITION",
    "TO_CENTURIES",
    "TO_BINARY",
    "LIST_REDUCE",
    "LIST_RESIZE",
    "TO_BASE64",
    "LIST_REVERSE_SORT",
    "LIST_SELECT",
    "TO_BASE",
    "TODAY",
    "LIST_SLICE",
    "LIST_SORT",
    "TIME_BUCKET",
    "TIMEZONE_MINUTE",
    "TIMEZONE_HOUR",
    "TIMEZONE",
    "LIST_TRANSFORM",
    "LIST_UNIQUE",
    "LIST_VALUE",
    "TAN",
    "MAP_KEYS",
    "LIST_WHERE",
    "LIST_ZIP",
    "LN",
    "LOG",
    "LOG10",
    "LOG2",
    "LOWER",
    "LPAD",
    "LTRIM",
    "MAP_FROM_ENTRIES",
    "MAKE_DATE",
    "MAKE_TIME",
    "MAKE_TIMESTAMP",
    "MAKE_TIMESTAMPTZ",
    "MAP",
    "MAP_CONCAT",
    "CURRENT_SETTING",
    "MEAN",
    "REGR_SYY",
    "SUM_NO_OVERFLOW",
    "LISTAGG",
    "MAX",
    "MAX_BY",
    "LIST",
    "LAST",
    "KURTOSIS_POP",
    "KURTOSIS",
    "SUMKAHAN",
    "MEDIAN",
    "SUM",
    "KAHAN_SUM",
    "HISTOGRAM",
    "MIN",
    "GROUP_CONCAT",
    "MIN_BY",
    "FSUM",
    "FIRST",
    "MODE",
    "FAVG",
    "ENTROPY",
    "VARIANCE",
    "VAR_POP",
    "VAR_SAMP",
    "COVAR_SAMP",
    "COVAR_POP",
    "COUNT_STAR",
    "COUNT",
    "CORR",
    "BOOL_OR",
    "BOOL_AND",
    "BIT_XOR",
    "BIT_OR",
    "BIT_AND",
    "BITSTRING_AGG",
    "AVG",
    "ARRAY_AGG",
    "ARG_MIN_NULL",
    "ARG_MIN",
    "ARG_MAX_NULL",
    "ARG_MAX",
    "PRODUCT",
    "QUANTILE",
    "QUANTILE_CONT",
    "QUANTILE_DISC",
    "ARGMIN",
    "ARGMAX",
    "ARBITRARY",
    "APPROX_QUANTILE",
    "APPROX_COUNT_DISTINCT",
    "ANY_VALUE",
    "STRING_AGG",
    "STDDEV_SAMP",
    "STDDEV_POP",
    "MAD",
    "STDDEV",
    "SKEWNESS",
    "SEM",
    "REGR_AVGX",
    "REGR_AVGY",
    "REGR_COUNT",
    "REGR_INTERCEPT",
    "REGR_R2",
    "REGR_SLOPE",
    "REGR_SXX",
    "REGR_SXY",
    "RESERVOIR_QUANTILE",
    "VERIFY_FETCH_ROW",
    "ALL_PROFILING_OUTPUT",
    "STORAGE_INFO",
    "SHOW_TABLES",
    "SHOW_DATABASES",
    "SHOW",
    "PLATFORM",
    "METADATA_INFO",
    "IMPORT_DATABASE",
    "FUNCTIONS",
    "ENABLE_VERIFICATION",
    "ENABLE_PROGRESS_BAR",
    "TABLE_INFO",
    "ENABLE_PROFILING",
    "ENABLE_PROFILE",
    "ENABLE_PRINT_PROGRESS_BAR",
    "ENABLE_OPTIMIZER",
    "ENABLE_OBJECT_CACHE",
    "ENABLE_CHECKPOINT_ON_SHUTDOWN",
    "DISABLE_VERIFY_SERIALIZER",
    "USER_AGENT",
    "DISABLE_VERIFY_PARALLELISM",
    "DISABLE_VERIFY_FETCH_ROW",
    "DISABLE_VERIFY_EXTERNAL",
    "DISABLE_VERIFICATION",
    "DISABLE_PROGRESS_BAR",
    "VERIFY_EXTERNAL",
    "VERIFY_PARALLELISM",
    "VERIFY_SERIALIZER",
    "DISABLE_PROFILING",
    "DISABLE_PROFILE",
    "DISABLE_PRINT_PROGRESS_BAR",
    "DISABLE_OPTIMIZER",
    "DISABLE_OBJECT_CACHE",
    "DISABLE_CHECKPOINT_ON_SHUTDOWN",
    "DATABASE_SIZE",
    "DATABASE_LIST",
    "COPY_DATABASE",
    "COLLATIONS",
    "SHOW_TABLES_EXPANDED",
    "&&",
    "SHOBJ_DESCRIPTION",
    "SESSION_USER",
    "ROUND_EVEN",
    "ROUNDBANKERS",
    "REGEXP_SPLIT_TO_TABLE",
    "NULLIF",
    "@>",
    "<@",
    "LIST_VAR_SAMP",
    "LIST_VAR_POP",
    "LIST_SUM",
    "LIST_STRING_AGG",
    "LIST_STDDEV_SAMP",
    "LIST_STDDEV_POP",
    "LIST_SKEWNESS",
    "LIST_SEM",
    "LIST_REVERSE",
    "LIST_PRODUCT",
    "LIST_PREPEND",
    "LIST_MODE",
    "LIST_MIN",
    "LIST_MEDIAN",
    "LIST_MAX",
    "LIST_MAD",
    "LIST_LAST",
    "LIST_KURTOSIS_POP",
    "LIST_KURTOSIS",
    "LIST_INTERSECT",
    "LIST_HISTOGRAM",
    "LIST_HAS_ANY",
    "LIST_HAS_ALL",
    "LIST_FIRST",
    "LIST_ENTROPY",
    "LIST_COUNT",
    "LIST_BOOL_OR",
    "LIST_BOOL_AND",
    "LIST_BIT_XOR",
    "LIST_BIT_OR",
    "LIST_BIT_AND",
    "LIST_AVG",
    "LIST_APPROX_COUNT_DISTINCT",
    "LIST_APPEND",
    "LIST_ANY_VALUE",
    "GET_BLOCK_SIZE",
    "GEOMETRIC_MEAN",
    "GEOMEAN",
    "GENERATE_SUBSCRIPTS",
    "FMOD",
    "USER",
    "FDIV",
    "DATE_ADD",
    "CURRENT_USER",
    "CURRENT_ROLE",
    "CURRENT_CATALOG",
    "COUNT_IF",
    "ARRAY_TO_STRING",
    "ARRAY_REVERSE",
    "ARRAY_PUSH_FRONT",
    "ARRAY_PUSH_BACK",
    "ARRAY_PREPEND",
    "ARRAY_POP_FRONT",
    "ARRAY_POP_BACK",
    "ARRAY_INTERSECT",
    "ARRAY_HAS_ANY",
    "ARRAY_HAS_ALL",
    "ARRAY_APPEND",
    "PG_TYPE_IS_VISIBLE",
    "PG_TYPEOF",
    "PG_TS_TEMPLATE_IS_VISIBLE",
    "PG_TS_PARSER_IS_VISIBLE",
    "PG_TS_DICT_IS_VISIBLE",
    "PG_TS_CONFIG_IS_VISIBLE",
    "PG_TABLE_IS_VISIBLE",
    "PG_SIZE_PRETTY",
    "PG_POSTMASTER_START_TIME",
    "PG_OPFAMILY_IS_VISIBLE",
    "PG_OPERATOR_IS_VISIBLE",
    "PG_OPCLASS_IS_VISIBLE",
    "PG_MY_TEMP_SCHEMA",
    "PG_IS_OTHER_TEMP_SCHEMA",
    "PG_HAS_ROLE",
    "PG_GET_VIEWDEF",
    "PG_GET_EXPR",
    "PG_GET_CONSTRAINTDEF",
    "PG_FUNCTION_IS_VISIBLE",
    "PG_CONVERSION_IS_VISIBLE",
    "PG_CONF_LOAD_TIME",
    "PG_COLLATION_IS_VISIBLE",
    "OBJ_DESCRIPTION",
    "INET_SERVER_PORT",
    "INET_SERVER_ADDR",
    "INET_CLIENT_PORT",
    "INET_CLIENT_ADDR",
    "HAS_TABLE_PRIVILEGE",
    "HAS_TABLESPACE_PRIVILEGE",
    "HAS_SERVER_PRIVILEGE",
    "HAS_SEQUENCE_PRIVILEGE",
    "HAS_SCHEMA_PRIVILEGE",
    "HAS_LANGUAGE_PRIVILEGE",
    "HAS_FUNCTION_PRIVILEGE",
    "HAS_FOREIGN_DATA_WRAPPER_PRIVILEGE",
    "HAS_DATABASE_PRIVILEGE",
    "HAS_COLUMN_PRIVILEGE",
    "HAS_ANY_COLUMN_PRIVILEGE",
    "FORMAT_TYPE",
    "FORMAT_PG_TYPE",
    "COL_DESCRIPTION",
    "SPLIT_PART",
  ],
  builtinVariables: [
    // Configuration
    "@@DATEFIRST",
    "@@DBTS",
    "@@LANGID",
    "@@LANGUAGE",
    "@@LOCK_TIMEOUT",
    "@@MAX_CONNECTIONS",
    "@@MAX_PRECISION",
    "@@NESTLEVEL",
    "@@OPTIONS",
    "@@REMSERVER",
    "@@SERVERNAME",
    "@@SERVICENAME",
    "@@SPID",
    "@@TEXTSIZE",
    "@@VERSION",
    // Cursor
    "@@CURSOR_ROWS",
    "@@FETCH_STATUS",
    // Datetime
    "@@DATEFIRST",
    // Metadata
    "@@PROCID",
    // System
    "@@ERROR",
    "@@IDENTITY",
    "@@ROWCOUNT",
    "@@TRANCOUNT",
    // Stats
    "@@CONNECTIONS",
    "@@CPU_BUSY",
    "@@IDLE",
    "@@IO_BUSY",
    "@@PACKET_ERRORS",
    "@@PACK_RECEIVED",
    "@@PACK_SENT",
    "@@TIMETICKS",
    "@@TOTAL_ERRORS",
    "@@TOTAL_READ",
    "@@TOTAL_WRITE",
  ],

  pseudoColumns: ["$ACTION", "$IDENTITY", "$ROWGUID", "$PARTITION"],
  tokenizer: {
    root: [
      { include: "@comments" },
      { include: "@whitespace" },
      { include: "@pseudoColumns" },
      { include: "@numbers" },
      { include: "@strings" },
      { include: "@complexIdentifiers" },
      { include: "@scopes" },
      [/[;,.]/, "delimiter"],
      [/[()]/, "@brackets"],
      [
        /[\w@#$]+/,
        {
          cases: {
            "@operators": "operator",
            "@builtinVariables": "predefined",
            "@builtinFunctions": "predefined",
            "@keywords": "keyword",
            "@default": "identifier",
          },
        },
      ],
      [/[<>=!%&+\-*/|~^]/, "operator"],
    ],
    whitespace: [[/\s+/, "white"]],
    comments: [
      [/--+.*/, "comment"],
      [/\/\*/, { token: "comment.quote", next: "@comment" }],
    ],
    comment: [
      [/[^*/]+/, "comment"],
      // Not supporting nested comments, as nested comments seem to not be standard?
      // i.e. http://stackoverflow.com/questions/728172/are-there-multiline-comment-delimiters-in-sql-that-are-vendor-agnostic
      // [/\/\*/, { token: 'comment.quote', next: '@push' }],    // nested comment not allowed :-(
      [/\*\//, { token: "comment.quote", next: "@pop" }],
      [/./, "comment"],
    ],
    pseudoColumns: [
      [
        /[$][A-Za-z_][\w@#$]*/,
        {
          cases: {
            "@pseudoColumns": "predefined",
            "@default": "identifier",
          },
        },
      ],
    ],
    numbers: [
      [/0[xX][0-9a-fA-F]*/, "number"],
      [/[$][+-]*\d*(\.\d*)?/, "number"],
      [/((\d+(\.\d*)?)|(\.\d+))([eE][\-+]?\d+)?/, "number"],
    ],
    strings: [
      [/N'/, { token: "string", next: "@string" }],
      [/'/, { token: "string", next: "@string" }],
    ],
    string: [
      [/[^']+/, "string"],
      [/''/, "string"],
      [/'/, { token: "string", next: "@pop" }],
    ],
    complexIdentifiers: [
      [/\[/, { token: "identifier.quote", next: "@bracketedIdentifier" }],
      [/"/, { token: "identifier.quote", next: "@quotedIdentifier" }],
    ],
    bracketedIdentifier: [
      [/[^\]]+/, "identifier"],
      [/]]/, "identifier"],
      [/]/, { token: "identifier.quote", next: "@pop" }],
    ],
    quotedIdentifier: [
      [/[^"]+/, "identifier"],
      [/""/, "identifier"],
      [/"/, { token: "identifier.quote", next: "@pop" }],
    ],
    scopes: [
      [/BEGIN\s+(DISTRIBUTED\s+)?TRAN(SACTION)?\b/i, "keyword"],
      [/BEGIN\s+TRY\b/i, { token: "keyword.try" }],
      [/END\s+TRY\b/i, { token: "keyword.try" }],
      [/BEGIN\s+CATCH\b/i, { token: "keyword.catch" }],
      [/END\s+CATCH\b/i, { token: "keyword.catch" }],
      [/(BEGIN|CASE)\b/i, { token: "keyword.block" }],
      [/END\b/i, { token: "keyword.block" }],
      [/WHEN\b/i, { token: "keyword.choice" }],
      [/THEN\b/i, { token: "keyword.choice" }],
    ],
  },
};
